package com.itheima.demo06JDBC用户登录_重点;

import java.sql.*;
import java.util.Scanner;

public class Demo06 {
	public static void main(String[] args) throws SQLException {
		// 1.使用数据库保存用户的账号和密码
		// 2.让用户输入账号和密码
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入账号");
		String username = sc.nextLine();
		System.out.println("请输入密码");
		String password = sc.nextLine();
		// 3.使用SQL根据用户的账号和密码去数据库查询数据
		Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql_day2", "root", "root");
		String sql = "SELECT * FROM user WHERE username='"+username+"' AND password='"+password+"'";
		Statement statement = connection.createStatement();
		ResultSet resultSet = statement.executeQuery(sql);

		// 4.如果查询到数据，说明登录成功
		if (resultSet.next()){
			System.out.println("登录成功");
		}else {
			System.out.println("登录失败");
		}
		// 5.如果查询不到数据，说明登录失败
		// 6.关闭资源
		resultSet.close();
		statement.close();
		connection.close();
	}
}
